//
//  VCOfSearch.m
//  ProjectA_ManualGuest
//
//  Created by dingcong on 16/6/29.
//  Copyright © 2016年 丁聪. All rights reserved.
//

#import "VCOfSearch.h"

#import "Masonry.h"

#import "NetWorkingHandle.h"

#import "ModelForSearch.h"

#import "CellOfSearch.h"

#import <WebKit/WebKit.h>

#import "VcOfWebView.h"
@interface VCOfSearch ()<UITableViewDelegate, UITableViewDataSource>



@property(nonatomic, retain)UITableView *tableView;

@property(nonatomic, retain)UIView *headerView;

@property(nonatomic, retain)UISearchBar *search;

@property(nonatomic, retain)NSMutableArray *mArrOfData;

@property(nonatomic, retain)NSMutableArray *mArrOfSearchData;
@end

@implementation VCOfSearch

- (void)dealloc{
    
    [_headerView release];
    [_tableView release];
    [_search release];
    
    [super dealloc];
    
    
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self creatHeaderView];
}


- (void)viewWillAppear:(BOOL)animated{
    
    self.navigationController.navigationBar.hidden = YES;
    
    
}
- (void)creatHeaderView{
    
    
    
    
    self.search = [[UISearchBar alloc]init];
    self.search.frame = CGRectMake(0, 20, SCREEN_WIDTH - SCREEN_WIDTH / 5, 64);
    self.search.barTintColor = [UIColor colorWithRed:0.0 / 256.0f  green:208.0 / 256.0f blue:252.0 / 256.0f alpha:1];
    self.search.alpha = 0.6;
    self.search.layer.cornerRadius = 19;
    
    self.search.placeholder = @"请输入您要查询的关键字";
    [self.view addSubview:self.search];
    
    
    
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.backgroundColor = [UIColor colorWithRed:0.0 / 256.0f  green:208.0 / 256.0f blue:252.0 / 256.0f alpha:1];
    button.alpha = 0.6;
    [self.view addSubview:button];
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).offset(20);
        make.leading.equalTo(self.search.mas_trailing).offset(0);
        make.height.offset(64);
        make.width.offset(SCREEN_WIDTH / 5);
    }];
    [button setImage:[UIImage imageNamed:@"11.png"] forState:UIControlStateNormal];
    [button setTintColor:[UIColor whiteColor]];
//    [button setTitle:@"搜索" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(handleSearch:) forControlEvents:UIControlEventTouchUpInside];
    
    UIImageView *imageOfBackground = [[UIImageView alloc]init];
    
    
    [self.view addSubview:imageOfBackground];
    [imageOfBackground mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.search.mas_bottom).offset(0);
        make.leading.equalTo(self.view.mas_leading).offset(0);
        make.width.offset(SCREEN_WIDTH);
        make.height.offset(SCREEN_HEIGHT / 3 + 20);
        
        
    }];
    imageOfBackground.image = [UIImage imageNamed:@"searchbeijing.png"];
    [imageOfBackground release];
    
}

- (void)handleSearch:(UIButton *)button{
    
    [self.search resignFirstResponder];
    
    [self Getdata];
    
    
    
    
}

//- (UISearchBar *)

- (void)Getdata{
    
    
    
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 84, SCREEN_WIDTH, SCREEN_HEIGHT - 84 - 49)];
    self.tableView.backgroundColor = [UIColor whiteColor];
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableView];
    [self.tableView registerClass:[CellOfSearch class] forCellReuseIdentifier:@"search"];
    
    
    
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    NSString *url = [NSString stringWithFormat:@"http://m.shougongke.com/index.php?&c=index&a=indexNewest&vid=18"];
    
    NetWorkingHandle *net = [[NetWorkingHandle alloc]init];
    
    [net netWorkingHandlerGetWithURL:url completion:^(id result, NSData *data, NSURLResponse *response, NSError *error) {
       
       
        self.mArrOfData = [NSMutableArray array];
        
        NSArray *arr = [[result objectForKey:@"data"] objectForKey:@"hotTopic"];
        
        
        
        for (NSDictionary *dic in arr)
        {
            ModelForSearch *model = [[ModelForSearch alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            
            NSRange found = [model.subject rangeOfString:self.search.text options:NSCaseInsensitiveSearch];
            
            
            if (found.length > 0)
            {
                NSLog(@"找到了");
                
                [self.mArrOfData addObject:model];
                
            }
            else {
                
                NSLog(@"没有找到");
            }
            
            
        }
        
        
        [self.tableView reloadData];
        
       
        

        
    }];
    
    
    [self.tableView release];
    
    
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    return 150;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    return self.mArrOfData.count;
    
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    [self.search resignFirstResponder];
    
    ModelForSearch *model = self.mArrOfData[indexPath.row];
    
    VcOfWebView *vc = [[VcOfWebView alloc]init];
    
    
    
    WKWebView *web = [[WKWebView alloc]initWithFrame:vc.view.frame];
    
    [web loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:model.mob_h5_url]]];
    
    [vc.view addSubview:web];
    
    [self presentViewController:vc animated:YES completion:^{
        
        UIButton *buttonOfBACK = [UIButton buttonWithType:UIButtonTypeCustom];
        
        buttonOfBACK.frame = CGRectMake(0, 20, 32, 32);
        [buttonOfBACK setImage:[UIImage imageNamed:@"zuo.png"] forState:UIControlStateNormal];
        
        [vc.view addSubview:buttonOfBACK];
        
        [buttonOfBACK addTarget:self action:@selector(Back:) forControlEvents:UIControlEventTouchUpInside];
        
    }];
    
    [vc release];
    [web release];
    
}

- (void)Back:(UIButton *)back{
    
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
    
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    CellOfSearch *cell = [tableView dequeueReusableCellWithIdentifier:@"search"];
    
    cell.model = self.mArrOfData[indexPath.row];
    
    return cell;
}


- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    [super touchesEnded:touches withEvent:event];
    
    [self.view endEditing:YES];
    
}

- (void)viewWillDisappear:(BOOL)animated{
    
    self.navigationController.navigationBar.hidden = NO;
    
}

- (void)viewDidDisappear:(BOOL)animated
{
    
    
    
    
    
}



@end
